/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package uk.ac.cam.can.watermarking.misc;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.InputStreamReader;
import java.io.FileInputStream;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.util.Random;

/**
 *
 * @author Thomas
 */
public class CSVDataSet implements DataSet{
    private final List<String[]> entries = new ArrayList<String[]>();
    
    public void readFromFile(String fileName, String separator) throws Exception{
        File f = new File(fileName);
        DataInputStream in = new DataInputStream(new FileInputStream(f));
        BufferedReader buff = new BufferedReader(new InputStreamReader(in));
        
        String strLine="";
        while ((strLine = buff.readLine()) != null)   {
            String[] attributes = strLine.split(separator);
            entries.add(attributes);
        }
    }
    
    public void writeToFile(String fileName, String separator) throws Exception{
        BufferedWriter buff = new BufferedWriter(new FileWriter(fileName));
        for(String[] entry : entries){
            for(int i = 0; i < entry.length-1;i++){
                    buff.write(entry[i]+separator);
                }
            buff.write(entry[entry.length-1]+separator);
            buff.newLine();
        }
        buff.flush();
        buff.close();
    }
    
    @Override
    public void setAttribute(int entryNb, int attributeNb, String value) throws IndexOutOfBoundsException{
        if(attributeNb < 0 || entryNb < 0)
            throw new IndexOutOfBoundsException("Value should not be negative");
        String[] attributes = entries.get(entryNb);
        attributes[attributeNb]=value.toString();
    }
    
    @Override
    public String getAttribute(int entryNb, int attributeNb) throws IndexOutOfBoundsException{
        if(attributeNb < 0 || entryNb < 0)
            throw new IndexOutOfBoundsException("Value should not be negative");
        String[] attributes = entries.get(entryNb);
        return attributes[attributeNb];
    }
    
    @Override
    public int size(){
        return entries.size();
    }
    
    @Override
    public int numberOfAttribute(){
        return entries.get(0).length;
    }

    @Override
    public boolean isNumeric(int attributeNb) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
    public void remove(double p){
        Random rand = new Random(System.nanoTime());
        int target=(int)(entries.size()*p);
        while(target>0){
            entries.remove(rand.nextInt(entries.size()));
            target--;
        }
    }
    
    public void round(int val){
        for(int i = 0; i < entries.size(); i++){
            String tmp = this.getAttribute(i, 1);
            int t = Integer.valueOf(tmp);
            t/=val;
            t*=val;
            setAttribute(i, 1, t+"");
        }
    }
}